package com.wude.guild;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WebActivity extends TitleBaseActivity {
    @BindView(R.id.webview)
    WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);

        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");

        if (!TextUtils.isEmpty(title)) {
            setHeaderTitle(title);
        } else {
            getTitleHeaderBar().getTitleTextView().setVisibility(View.GONE);
        }

        initdata(url);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initdata(String url) {
        WebSettings settings = webview.getSettings();
        //适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        //隐藏原生的缩放控件
        settings.setDisplayZoomControls(false);
        //设置是否支持JS
        settings.setJavaScriptEnabled(true);

        if (Utils.getLoginUser() != null) {
            String userAgentString = settings.getUserAgentString();
            userAgentString = userAgentString + "&appToken=" + Utils.getLoginUser().token;
            settings.setUserAgentString(userAgentString);

            webview.addJavascriptInterface(new Questionnaire(), "Questionnaire");
        }

        webview.loadUrl(url);
        webview.setWebViewClient(new WebViewSetting());
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    class WebViewSetting extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            //想在页面开始加载时有操作，在这添加
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            //想在页面加载结束时有操作，在这添加
            super.onPageFinished(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //返回值是true的时候WebView打开，为false则系统浏览器或第三方浏览器打开。如果要下载页面中的游戏或者继续点击网页中的链接进入下一个网页的话，重写此方法下，不然就会跳到手机自带的浏览器了，而不继续在你这个webview里面展现了
            return true;
        }

        @Override

        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {

            //想在收到错误信息的时候，执行一些操作，走此方法

            Log.d("webErrorCode", "++++++++++++++++++++++++" + errorCode);

        }
    }

    private class Questionnaire extends Object {
        @JavascriptInterface
        public void commit(String resultJson) {
            Log.d("webwebweb", "-----------------" + resultJson);

            try {
                JSONObject jsonObject = new JSONObject(resultJson);
                int status = jsonObject.getInt("status");
                String message = jsonObject.getString("message");
                ToastUtil.showToast(message);
                if (status == 1) {
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
